// Package webmanage @author <chengjiang@buffalo-robot.com>
// @date 2023/2/21
// @note
package webmanage

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"gorm.io/gorm"
	"phm/internal/biz"
	"phm/internal/biz/webmanage"
	"phm/models/generator/model"
	"phm/models/generator/query"
)

type sysPermissionRepo struct {
	Db  biz.DataRepo
	log *log.Helper
}

func NewSysSysPermissionRepo(data biz.DataRepo, logger log.Logger) webmanage.SysPermissionRepo {
	return &sysPermissionRepo{Db: data, log: log.NewHelper(logger)}
}

func (u *sysPermissionRepo) CreateSysPermission(ctx context.Context, uModel *model.SysPermission, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).SysPermission
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *sysPermissionRepo) UpdateSysPermission(ctx context.Context, uModel *model.SysPermission, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).SysPermission
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新角色错误：%v", err)
		return err
	}
	return nil
}

func (u *sysPermissionRepo) DeleteSysPermissionByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).SysPermission
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *sysPermissionRepo) GetSysPermissionById(ctx context.Context, id string) (*model.SysPermission, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysPermission
	permission, err := opt.WithContext(ctx).Where(opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return permission, nil
}

func (u *sysPermissionRepo) GetSysPermissionByIds(ctx context.Context, ids []string) ([]*model.SysPermission, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysPermission
	permissions, err := opt.WithContext(ctx).Where(opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return permissions, nil
}

func (u *sysPermissionRepo) GetSysPermissionByRoleId(ctx context.Context, roleId string) ([]*model.SysPermission, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysRolePermission
	rolePermissions, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.RoleID.Eq(roleId)).Find()
	if err != nil {
		return nil, err
	}
	var ids = []string{}
	for _, v := range rolePermissions {
		ids = append(ids, *v.PermissionID)
	}
	operation := query.Use(s).SysPermission
	if len(ids) < 1 {
		operations, err := operation.WithContext(ctx).Where(operation.IsSoftDelete.Eq(0)).Order(operation.Sort).Find()
		if err != nil {
			return nil, err
		}
		return operations, nil
	} else {
		operations, err := operation.WithContext(ctx).Where(operation.IsSoftDelete.Eq(0), operation.ID.In(ids...)).Order(operation.Sort).Find()
		if err != nil {
			return nil, err
		}
		return operations, nil
	}
}

func (u *sysPermissionRepo) GetDB() *gorm.DB {
	return u.Db.GetDB()
}
